local jixia = fk.CreateSkill{
    name = "XD__jixia",
}
Fk:loadTranslationTable{
    ["XD__jixia"] = "击瑕",
    [":XD__jixia"] = "回合技，当你攻击范围内的角色使用【闪】响应【杀】时，你可以与其拼点且你可以令其将此【闪】当拼点牌，若你赢，此【闪】无效，其可以重新使用【闪】，且若其未重新使用【闪】，其本次受到【杀】造成的伤害时，此伤害+1。",
    ["#XD__jixia"] = "是否发动 击瑕，尝试无效 %dest 使用的【闪】",
    ["#XD__jixia1"] = "与 %dest 拼点，你赢则此【闪】无效",
    ["#XD__jixia2"] = "令 %dest 改为使用%arg进行拼点",

    ["$XD__jixia1"] = "哼，你再无苟活之日！",
    ["$XD__jixia2"] = "谁生谁死，即见分晓！",
}

jixia:addEffect(fk.CardUsing, {
    anim_type = "control",
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(jixia.name) and data.card.name == "jink" and player:inMyAttackRange(target) and data.responseToEvent and data.responseToEvent.card
        and data.responseToEvent.card.trueName == "slash" and player:usedSkillTimes(jixia.name, Player.HistoryTurn) == 0 and player:canPindian(target, false, true)
    end,
    on_cost = function (self, event, target, player, data)
        local choices = {}
        if player:canPindian(target) then table.insert(choices, "#XD__jixia1::"..target.id) end
        if #Card:getIdList(data.card) == 1 then table.insert(choices, "#XD__jixia2::"..target.id..":"..data.card:toLogString()) end
        if #choices > 0 then
            table.insert(choices, "Cancel")
            local choice = player.room:askToChoice(player, {
                choices = choices,
                all_choices = {"#XD__jixia1::"..target.id, "#XD__jixia2::"..target.id..":"..data.card:toLogString(), "Cancel"},
                skill_name = jixia.name,
                prompt = "#XD__jixia::"..target.id
            })
            if choice ~= "Cancel" then
                event:setCostData(self, choice)
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        if event:getCostData(self):startsWith("#XD__jixia2") then
            room:setPlayerMark(target, "XD__jixia-phase", Card:getIdList(data.card)[1])
        end
        local pindian = player:pindian({target}, jixia.name)
        room:setPlayerMark(target, "XD__jixia-phase", 0)
        if pindian.results[target].winner == player then
            data.toCard = nil
            local params = { ---@type AskToUseCardParams
                pattern = "jink",
                skill_name = "jink",
                prompt = "#slash-jink:" .. data.responseToEvent.from.id,
                cancelable = true,
                event_data = data.responseToEvent
            }
            local use = room:askToUseCard(target, params)
            if use then
                room:useCard(use)
            else
                local use_event = room.logic:getCurrentEvent().parent:findParent(GameEvent.UseCard)
                if use_event == nil then return end
                local use_data = use_event.data
                use_data.extra_data = use_data.extra_data or {}
                use_data.extra_data.XD__jixia = use_data.extra_data.XD__jixia or {}
                table.insert(use_data.extra_data.XD__jixia , target.id)
            end
        end
    end,
})

jixia:addEffect(fk.StartPindian, {
    can_refresh = function(self, event, target, player, data)
        if player == data.from then
        for _, to in ipairs(data.tos) do
            if to:getMark("XD__jixia-phase") ~= 0 then
            return true
            end
        end
        end
    end,
    on_refresh = function(self, event, target, player, data)
        if player == data.from then
            for _, to in ipairs(data.tos) do
                if not to.dead and to:getMark("XD__jixia-phase") ~= 0 then
                    data.results[to] = data.results[to] or {}
                    data.results[to].toCard = Fk:getCardById(to:getMark("XD__jixia-phase"))
                end
            end
        end
    end,
})

jixia:addEffect(fk.DamageInflicted, {
    can_refresh = function(self, event, target, player, data)
        if target == player and data.card then
            local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
            if use_event == nil then return end
            local use = use_event.data
            return use.extra_data and use.extra_data.XD__jixia and table.contains(use.extra_data.XD__jixia, player.id)
        end
    end,
    on_refresh = function(self, event, target, player, data)
        local use_event = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        local use = use_event.data
        local num = #table.filter(use.extra_data.XD__jixia,function (element)
            return element == player.id
        end)
        data:changeDamage(num)
    end,
})

return jixia